﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Core.Notifications
{
    public class Notification
    {

        
    

        public static void AddNotification(int userId, string text, string metaData, ref Model.HangoutDBEntities context)
        {
            //add a notification.
            Model.Notification n = new Model.Notification();
            n.DateTimeStamp = DateTime.Now;
            n.MarkAsRead = false;
            n.MetaData = metaData;
            n.Text = text;
            n.UserID = userId;

            context.AddToNotifications(n);
            context.SaveChanges();


            //send the notification down to devices. 

            PushNotifications.PushNotifications.SendNotification(n,context);

        }

        public static void AddNotification(List<int> userIds, string text, string metaData, ref Model.HangoutDBEntities context)
        {
            foreach (int id in userIds)
            {
                AddNotification(id, text, metaData, ref context);
            }

        }

        public static void MarkAsRead(int notificationId, ref Model.HangoutDBEntities context)
        {
            Model.Notification n = context.Notifications.Where(o => o.NotificationID == notificationId).SingleOrDefault();
            if (n != null)
            {
                n.MarkAsRead = true;
            }

            context.SaveChanges();
        }

        public static List<Model.Notification> GetNotifications(int userId, int take, int skip, Model.HangoutDBEntities context)
        {
            return context.Notifications.Where(o => o.UserID == userId).OrderByDescending(o => o.DateTimeStamp).Skip(skip).Take(take).ToList();
        }

        public static void MarkAsRead(List<int> notiifcationIds, ref Model.HangoutDBEntities context)
        {
            foreach (int a in notiifcationIds)
            {
                MarkAsRead(a, ref context);
            }
        }

        public static bool? IsRead(int notificationId, Model.HangoutDBEntities context)
        {
            Model.Notification n = GetNotification(notificationId, context);
            if (n == null)
            {
                return null;
            }

            return n.MarkAsRead;
        }

        public static Model.Notification GetNotification(int notificationId, Model.HangoutDBEntities context)
        {
            return context.Notifications.Where(o => o.NotificationID == notificationId).SingleOrDefault();
        }

        public static int GetUnreadNotificationCount(int userId, Model.HangoutDBEntities context)
        {
            return context.Notifications.Where(o => o.UserID == userId && o.MarkAsRead == false).Count();
        }

        public static void MarkAllNotificationsAsRead(int userId,ref Model.HangoutDBEntities context)
        {
            List<Model.Notification> notification = context.Notifications.Where(o => o.UserID == userId && o.MarkAsRead == false).ToList();

            foreach (Model.Notification n in notification)
            {
                n.MarkAsRead = true;
            }

            context.SaveChanges();
           
        }
    }
}